package org.zjx.app.action.task;

import org.zjx.analysis.analyze.game.GameAnalysis;
import org.zjx.analysis.analyze.window.impl.DialogueAnalyzer;
import org.zjx.app.action.route.BaseRouteAction;
import org.zjx.app.action.window.ClickSceneAction;
import org.zjx.app.action.window.DialogueAction;
import org.zjx.command.entity.screen.Address;
import org.zjx.command.entity.screen.Npc;
import org.zjx.window.action.Action;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ClaimTaskAction extends Action {
    private final Npc npc;
    private final List<String> dialogues;

    public ClaimTaskAction(IWindow targetWindow, Npc npc,List<String> dialogues) {
        super(targetWindow, false);
        this.npc = npc;
        this.dialogues = dialogues;
    }

    @Override
    protected void doExecute() throws Exception {
        GameAnalysis gameAnalysis = new GameAnalysis(targetWindow);
        Address address = gameAnalysis.getAddress();
        if (!npc.getMainLocation().near(address,5)){
            targetWindow.submitAction(new BaseRouteAction(targetWindow,npc.getMainLocation()));
            return;
        }
        DialogueAnalyzer dialogueAnalyzer = new DialogueAnalyzer(targetWindow);
        if (dialogueAnalyzer.isOpened()){
            targetWindow.submitAction(new DialogueAction(targetWindow,dialogues));
            targetWindow.getState().getGameState().setCommand(null);
            targetWindow.getState().getGameState().setGatherQuest(false);
        }else {
            List<Point> points = npc.getMovePath();
            List<Address> addressesList = new ArrayList<>();
            for (Point point : points) {
                addressesList.add(new Address(npc.getSceneName(),point.x,point.y));
            }
            targetWindow.submitAction(new ClickSceneAction(targetWindow,addressesList, ClickSceneAction.ClickType.NPC));
        }

    }
}
